package com.xk.ucs.service;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.xk.ucs.common.ErrorEnum;
import com.xk.ucs.common.UrlConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminRemoteService
 * @Description 远程接口服务
 * @Author zhouLiang
 * @Date 2024/9/6 17:40
 */
@Service
@Slf4j
public class AdminRemoteService {
    @Value("${admin.url}")
    private String adminUrl;

    private String getFullUrl(String path){
        if(StrUtil.isBlank(path)){
            throw ErrorEnum.PARAM_ERROR.expMsg("远程服务路径不能为空");
        }
        if(StrUtil.startWithIgnoreCase(path,"http")){
            return path;
        }
        if(StrUtil.startWith(adminUrl,"/")){
            adminUrl = StrUtil.removeSuffix(adminUrl,"/");
        }
        return adminUrl+StrUtil.addPrefixIfNot(path,"/");
    }

    public String login(Map<String,Object> req){
        return HttpUtil.post(getFullUrl(UrlConstant.LOGIN), JSONUtil.toJsonStr(req));
    }
    public Map<String,Object> getGoods(Map<String,Object> req) throws Exception{
        String result = HttpUtil.post(getFullUrl(UrlConstant.GET_GOODS), JSONUtil.toJsonStr(req));
        return JSONUtil.toBean(result, Map.class);
    }
    public Map<String,Object> getGoodsByBarCode(String barCode){
        String result = HttpUtil.post(getFullUrl(UrlConstant.GET_GOODS), barCode);
        return JSONUtil.toBean(result, Map.class);
    }

    public List<Map<String,Object>> listGoods(Map<String,Object> req){
        String result = HttpUtil.post(getFullUrl(UrlConstant.LIST_GOODS), req);
        return JSONUtil.toBean(result, List.class);
    }


    public Map<String,Object> listDict(Map<String,Object> req){
        String result = HttpUtil.post(adminUrl, req);
        return JSONUtil.toBean(result, Map.class);
    }
    public String getValueByKey(String groupCode,String dictCode){
        Map<String,Object> map = new HashMap<>();
        map.put("groupCode",groupCode);
        map.put("dictCode",dictCode);
        String result = HttpUtil.post(adminUrl, map);
        Map<String,Object> resMap = JSONUtil.toBean(result, Map.class);
        return resMap==null?"":resMap.get("dictValue").toString();
    }
    public Map<String,String> getValueByGroup(String groupCode){
        Map<String,Object> map = new HashMap<>();
        map.put("groupCode",groupCode);
        String result = HttpUtil.post(adminUrl, map);
        return JSONUtil.toBean(result, Map.class);
    }

    public Map<String,Object> getBox(Integer boxNo){
        Map<String,Object> map = new HashMap<>();
        map.put("boxNo",boxNo);
        String result = HttpUtil.post(adminUrl+"/box/get", map);
        return JSONUtil.toBean(result, Map.class);
    }

    public boolean updateBox(Map<String, Object> box) {
        return true;
//        String result = HttpUtil.post(adminUrl+"", box);
//        return JSONUtil.toBean(result, Boolean.class);
    }
}
